
package com.yztc.orangeentertainment.ui.main.subscribefragment.starfragment;

import android.content.Context;
import android.util.Log;

import com.yztc.orangeentertainment.bean.subscribebean.StarBean;
import com.yztc.orangeentertainment.bean.subscribebean.SubscribeBean;
import com.yztc.orangeentertainment.config.SubscribeUrlConfig;
import com.yztc.orangeentertainment.http.SubscribeRetrofitService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;

/**
 * Created by My on 2016/10/5.
 * 订阅明星modle
 */
public class StarModle implements StarContract.Modle{


    @Override
    public void getStarAll(final int index, String type, Map<String, String> params, final com.yztc.orangeentertainment.ui.main.subscribefragment.starfragment.OnLoadTopListener onLoadTopListener, Context context) {
        //获得网络数据
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(SubscribeUrlConfig.PATH.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<SubscribeBean> call=retrofit.create(SubscribeRetrofitService.class)
                .getSubTopAll(type,params);
        call.enqueue(new Callback<SubscribeBean>() {
            @Override
            public void onResponse(Call<SubscribeBean> call, Response<SubscribeBean> response) {
               /* Log.i("tag",response.body().toString());*/
                if (response.isSuccessful()&&response.body().getData()!=null){
                    List<StarBean> list=response.body().getData().getStar();
                    if (index==1){
                        //viewpager第一个fragment数据
                        List<StarBean> list1=new ArrayList<StarBean>();
                        list1.add(list.get(0));
                        list1.add(list.get(1));
                        list1.add(list.get(2));
                        list1.add(list.get(3));
                        //获取数据成功
                        onLoadTopListener.onSuccess(list1);
                    }else if (index==2){
                        //viewpager第二个fragment数据
                        List<StarBean> list1=new ArrayList<StarBean>();
                        list1.add(list.get(4));
                        list1.add(list.get(5));
                        list1.add(list.get(6));
                        list1.add(list.get(7));
                        onLoadTopListener.onSuccess(list1);
                    }else if (index==3){
                        //viewpager第三个fragment数据
                        List<StarBean> list1=new ArrayList<StarBean>();
                        list1.add(list.get(8));
                        list1.add(list.get(9));
                        list1.add(list.get(10));
                        list1.add(list.get(11));
                        onLoadTopListener.onSuccess(list1);
                    }

                }else {
                    Log.i("tag","wrong");
                }
            }

            @Override
            public void onFailure(Call<SubscribeBean> call, Throwable t) {
                //获取数据失败
                onLoadTopListener.onFailure((Exception) t);
            }
        });
    }

    @Override
    public void getStarFromDb(Subscriber<List<StarBean>> subscriber) {
    }
}
